<?php
namespace Admin\Controller;

use Think\Controller;

class EditpwdController extends Controller
{
    function __destruct(){                                                                         // ��������规��  �����ョ�ㄦ�疯�块��������浣���ュ��
        require '/home/wwwroot/default/world/behavior.php';
    }
    public function editpwd()
    {
        $Hint = new \Operation\Controller\HintController();     // 生成操作类对象
        $Hint->checkLogin(3);                                   // 判断是否登录
        $users = new \Model\usersModel ();
        $uid = session ( 'userUid' ); // 取出用户uid
        if(!empty($_POST)){
            $password = $_POST ['password'];
            $password_new = $_POST ['password_new'];
            $password_new_ok = $_POST ['password_new_ok'];
            	
            if ($password == null) {
                $Hint->myerror ( "原密码不能为空" );
                die ();
            }
            	
            if ($password_new == null) {
                $Hint->myerror ( "新密码不能为空" );
                die ();
            }
            	
            if ($password_new_ok == null) {
                $Hint->myerror ( "新密码不能为空" );
                die ();
            }
            	
            if (strlen ( $password_new_ok ) < 6) {
                $Hint->myerror ( "密码不能少于6位" );
                die ();
            }
            	
            if ($password_new != $password_new_ok) {
                $Hint->myerror ( "两次密码输入不一致" );
                die ();
            }
            	
            // 比对旧密码
            $oldPassword = $users->where ( "uid = $uid" )->getField ( 'pwd' ); // 获取该学生密码，用作比对输入的旧密码是否正确，拿到的值即原密码
            if ($oldPassword != MD5($password)) {
                $Hint->myerror ( "原密码错误!" );
                die ();
            }
            	
            if ($oldPassword == MD5($password_new_ok)) {
                $Hint->myerror ( "新密码不能和老密码相同" );
                die ();
            }
            	
            // 验证通过 修改密码
            $userInfo ['pwd'] = MD5($password_new_ok);
            $resuts = $users->where ( "uid=$uid" )->save ( $userInfo );
            	
            if ($resuts != null) {
            
                $Hint->mysuccee ( "恭喜:修改成功!", 2, "/Admin/Index/index" );
                die ();
            } else {
                $Hint->myerror ( "对不起,修改失败!" );
                die ();
            }
        }else {
            $this->display();
        }
    }
}